<template>
 	<view class="Page">
 		<view class="title">已扫码列表</view>
 		<view class="noData" v-show="list.length==0">当前无扫码数据</view>
 		<view v-show="list.length!=0" class="is_show_box">
 			<view>
 				<view class="data_item" v-for="(item ,i) in list" :key="i">
 
 						<view class="card-left">
 							<view><text class="gray">物料名称：</text>{{item.spare_manage__name}}</view>
 							<view><text class="gray">物料编码：</text>{{item.spare_manage__code}}</view>
 							<view style="font-weight:600 ;">库存数量({{item.spare_manage__unit__name}})：<text style="color: red;"> {{item.num}}</text></view>
 							<view><text class="gray">规格：</text>{{item.spare_manage__specs }} {{' '}}<text class="gray">型号：</text>{{item.spare_manage__model}}</view>
 							<view><text class="gray">库存属性：</text>{{item.now_spare_status__name }} {{' '}}<text class="gray">流水号：</text>{{item.no}}</view>
							<view><text class="gray">库存位置：</text>{{list.store_place__flag}}</view>
 						</view>
 						<button size="mini" type="warn" @click="delete_fun(i)" style="font-size: 14px;">删除</button>
 				</view>
 				
 				
 			</view>	
 
 		</view>
		<view class="title">备注</view>
		<textarea placeholder="请输入备注" v-model="bz"></textarea>
		<uni-popup ref="is_oldEnter" type="dialog">
			<uni-popup-dialog type="success" cancelText="取消" confirmText="确定" title="通知" content="是否确定入待维修库" @confirm="oldEnterConfirm"
				@close="isMoveClose"></uni-popup-dialog>
		</uni-popup>
 		<view class="kongbai">
 			
 		</view>
 	</view>
	<view class="input_goal"><label style="font-size: 14px;">目标库位：</label><input v-model="location" style="font-size: 14px;"/><image src="../../static/scan_icon.svg" mode="aspectFit" @click="scan_goal"></image></view>
	<view class="buttonBox">
		<button size="default" type="primary"  @click="oldEnter">入待维修库</button>
		<button size="default" type="default"  @click="scan1" class="scanButton">扫码</button>
	</view>
 </template>
 
 <script>
 	export default {
 		data() {
 			return {
 				id:123,
 				// is_show:false,
 				code:1234,
 				// 二维码查询出的码
 				qr_code:"",
 				list:[],
 				return_data:[],
 				org_id:34,
				choosedID:'',
				location:'',
				if_scan:false,
				is_find:false,
				bz:'',
 			}
 		},
		watch: {
		    location: {
		        handler(oldValue,newVal) {
					console.log(this.if_scan)
					if(this.if_scan){
						this.if_scan=false;
					}
					else{
						this.choosedID='';
					}
		        }
		    }
		},
		computed:{
			getNowFormatDate() {
			  let date = new Date(),
			    year = date.getFullYear(), //获取完整的年份(4位)
			    month = date.getMonth() + 1, //获取当前月份(0-11,0代表1月)
			    strDate = date.getDate() // 获取当前日(1-31)
			  if (month < 10) month = `0${month}` // 如果月份是个位数，在前面补0
			  if (strDate < 10) strDate = `0${strDate}` // 如果日是个位数，在前面补0
			 
			  return `${year}-${month}-${strDate}`
			}
		},
 		onLoad() {
 			this.scan1();
 		},
 		methods: {
 			scan1(){
 				let that=this;
 				uni.scanCode({
 					scanType: ['qrCode'],
 					success: function (res) {
 						console.log(res.result)
 						let strArray=res.result.split(',')
 						console.log(strArray)
 						if(strArray[0][5]!='d'){
 							console.log('扫的码不正确')
 							// that.$refs.mabucunzai.open('center')
							that.$root.message('fail','扫的码不是本系统的码')
 						}
 						else{
 							console.log('扫的码正确')
 							that.qr_code=strArray[0].slice(12);
 							that.org_id=strArray[2].slice(7);
 							console.log(that.qr_code);
 							console.log(that.org_id);
								that.$root.http('http://113.15.231.21:180/storage_manage2/ding_app/query_spare_device_list',
									{
										org_id:that.org_id, 
										ids:that.qr_code, 
									}, 
									{
									  	sessionid:that.$root.sessionID
									})
									.then((res) => {
										let fun=function(){
											if(res.data.result&&res.data.result.list&&res.data.result.list.length>0){
												console.log("存在");
												console.log(res.data.result.list[0]);
												
												if(res.data.result.list[0]?.store_status == 'wait'){
													that.list.push(res.data.result.list[0]);
													// 返回服务端的数组
													
													that.return_data.push({
														spare_status_flag: 'dai_xiu', // 库存状态
														spare_device_id: res.data.result.list[0].id,      // 流水码ID（单个实物ID）
														num: 1,                  // 领料数量
													})
													console.log("r1");
													console.log(that.return_data);
													// 获取的去重
													let map = new Map();
													for (let item of that.list) {
														map.set(item.id, item);
													 }
													that.list = [...map.values()];
													console.log("c2");
													console.log(that.list);
													// 返回的去重
													let map1 = new Map();
													for (let item of that.return_data) {
														map1.set(item.spare_device_id, item);
													 }
													that.return_data = [...map1.values()];
													console.log("r2");
													console.log(that.return_data);
													
												}
												else{
													// that.$refs.kucun0.open('center')
													that.$root.message('fail','该物料不是下线待处理件')
												}
			 
											}
											else{
												console.log("不存在");
												that.$root.message('fail','扫的码id不存在')
											}
										}
										that.$root.judge(res,fun);
									})
 						}
 					}
 				});	
 				
 			},
 			delete_fun(i){
 				this.list.splice(i, 1);
 				this.return_data.splice(i, 1);
 				console.log('删除后的')
 				console.log(this.return_data);
 			},
			scan_goal(){
				let that=this;
				uni.scanCode({
					scanType: ['qrCode'],
					success: function (res) {
						console.log(res.result)
						that.if_scan=true;
						let strArray=res.result.split(',')
						console.log(strArray)
						if(strArray[0][5]!='g'){
							that.$root.message('fail','扫的码不是位置码')
						}
						else{
							that.choosedID=strArray[0].slice(14);
							that.location=strArray[1].slice(5);
							console.log(that.choosedID);
							console.log('location'+that.location);
						}
				 
					},
				});
			},
			oldEnter(){

				if(this.return_data.length>0&&this.list.length>0){
					if(this.choosedID==''){
						let that=this;
						that.$root.http('http://113.15.231.21:180/storage_manage2/ding_app/query_storage_place_info_list',
							{
								org_id:34,
							}, 
							{
							  	sessionid:that.$root.sessionID
							})
							.then((res) => {
								let fun=function(){
									that.storageList=res.data.result.list;
									// console.log(that.storageList[0].name);
									for(var item in res.data.result.list){
										if(item.name==that.location){
											that.choosedID=item.id;
											that.is_find=true;
										}
									}
									if(!that.is_find){
										that.$root.message('fail','目标位置错误,请选择正确的目标位置')
									}
									else{
										that.$refs.is_oldEnter.open('center')
									}
								}
								that.$root.judge(res,fun);
							})
					}
					else{
						this.$refs.is_oldEnter.open('center')
					}
					
				}
				else{
					that.$root.message('fail','请先扫码添加物料')
				}
			},
			oldEnterConfirm(){

					let that=this;
					that.$root.http('http://113.15.231.21:180/storage_manage2/ding_app/create_spare_record_by_normal',
						{
							org_id:that.org_id,               // 组织ID
							record_type_flag: 'dai_xiu_in',   // 业务类型，这里写死
						   store_place_id:that.choosedID,       // 目标库位
						   date:that.getNowFormatDate,        // 领用时间，默认当天
						   data:JSON.stringify(that.return_data),
						   bz:that.bz
						}, 
						{
						  	sessionid:that.$root.sessionID
						})
						.then((res) => {
							let fun=function(){
								that.list=[];
								that.return_data=[];
							}
							that.$root.judge(res,fun);
						})

				
			},
			
 		}
 	}
 </script>
 
 <style scoped>
 	.Page{
 		width: 100%;
 		font-size: 14px;
  		background-color: #F6F6F6;
 		display: flex;
 		flex-direction: column;
 		/* align-items: center; */
		padding-left: 10px;
		padding-right: 10px;
 		
 	}
 	.title{
 		font-size: 16px;
 		margin-top: 10px;
 		font-weight: 600;
		border-left: 2px solid dodgerblue;
		padding-left: 8px ;
 	}
 	.noData{
 		margin-top: 15px;
 	}
 	textarea{
		margin-top: 15px;
		width: 93%;
		font-size: 14px;
	}
 
 	.data_item{
 		margin-top: 12px;
 		width: 90%;
 		background-color: white;
 		border-radius: 5px;
 		font-size: 14px;
 		padding-left: 10px;
 		padding-bottom: 5px;
 		display: flex;
 		flex-direction: row;
 		justify-content: space-between;
 		
 	}
 	.card-left view{
 		margin-top: 5px;
 	}
 	.gray{
 		color: #93A1A1;
 	}
 	.num-icon{
 		width: 16px;
 		height: 16px;
 	}
 	.dialog_center{
 		display: flex;
 		flex-direction: row;
 		justify-content: center;
 		align-items: center;
 	}
 	.is_show_box{
 		width: 100%;
 	}
	.input_goal{
		position: fixed;
		bottom: 49px;
		width: 100%;
		height: 40px;
		background-color: white;
		line-height: 40px;
		display: flex;
		justify-content: space-around;
		align-items: center;
	}
	.input_goal input{
		border: 1px solid gainsboro;
		width: 55%;
		height: 30px;
	}
	.input_goal image{
		width: 12%;
		height: 40px;
		/* background-color: pink; */
	}
 	.kongbai{
 		width: 100%;
 		height: 60px;
 	}
 	.buttonBox{
 		width: 100%;
 		position: fixed;
 		bottom: 0%;
 		display: flex;
 		flex-direction: row;
 		justify-content: space-around;
 		align-items: center;
 	}
 	.buttonBox button{
 		width: 50%;
 	}
 	.scanButton{
 		background-color: #42B983;
 		/* background-color: #41A863; */
 		color: white;
 		border: 1px solid #42B983;
 	}
 </style>
 